package com.ta.stage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ta.stage.dao.PolicyDao;
import com.ta.stage.entity.Menu;
import com.ta.stage.entity.Policy;
import com.ta.stage.entity.vo.PolicyVo;
import com.ta.stage.result.Result;
import com.ta.stage.service.PolicyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/policy/")
@Api(tags={"报警策略 PolicyController"})
public class PolicyController {

    @Autowired
    private PolicyService policyService;

    @ApiOperation(value = "查询报警策略")
    @RequestMapping(value = "findPolicy",method = RequestMethod.GET)
    @ResponseBody
    public Result findPolicy(){
        List<Policy> policyList = policyService.list(null);
        return Result.ok(policyList);
    }

    @ApiOperation(value = "根据id查询报警策略")
    @GetMapping("getPolicy/{id}")
    public Result getPolicy(@PathVariable Long id){
        Policy policy = policyService.getById(id);
        return Result.ok(policy);
    }

    @ApiOperation(value = "新增报警策略")
    @RequestMapping(value = "savePolicy",method = RequestMethod.POST)
    @ResponseBody
    public Result savePolicy(@RequestBody Policy policy){
        boolean save = policyService.save(policy);
        return save?Result.ok():Result.fail();
    }

    @ApiOperation(value = "逻辑删除报警策略")
    @DeleteMapping("{id}")
    public Result removePolicy(@PathVariable Long id){
        boolean flag = policyService.removeById(id);
        return flag?Result.ok():Result.fail();
    }

    @ApiOperation(value = "修改报警策略")
    @PostMapping("updatePolicy")
    public Result updatePolicy(@RequestBody Policy policy){
        boolean flag = policyService.updateById(policy);
        return flag?Result.ok():Result.fail();
    }

    @ApiOperation(value = "批量删除报警策略")
    @DeleteMapping("batchRemovePolicy")
    public Result batchRemovePolicy(@RequestBody List<Long> idList){
        policyService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "报警策略条件查询带分页")
    @PostMapping("findPagePolicy/{current}/{limit}")
    public Result findPagePolicy(@PathVariable long current,
                               @PathVariable long limit,
                               @RequestBody(required = false) PolicyVo policyVo){

        //创建page对象，传递当前页，每页记录数
        Page<Policy> page = new Page<>(current,limit);

        //构建条件
        QueryWrapper<Policy> wrapper = new QueryWrapper<>();
        Integer id = policyVo.getId();
        String name = policyVo.getName();
        //条件可能为空
        if (!StringUtils.isEmpty(id)){
            wrapper.eq("id",policyVo.getId());
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",policyVo.getName());
        }
        //调用方法实现分页查询
        Page<Policy> pageDept = policyService.page(page, wrapper);
        return Result.ok(pageDept);
    }
}

